import { INITIAL_FORM_VALUE, GOODS_TYPES } from '@/views/price-base-goods/constants';
import { save, update, getGoodsName } from './api';
import { message } from '@/utils/util';
import { parseToObject } from '@/utils';

export default {
  name: 'price-base-goods-edit-modal',
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    formValueString: String,
    topChannelList: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      formValue: { id: null },
      goodsTypeList: GOODS_TYPES,
      formRules: {
        topChannelCode: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.formValue.topChannelCode) {
                callback('顶级渠道不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        companyCode: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.formValue.companyCode) {
                callback('公司编码不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        goodsCode: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.formValue.goodsCode) {
                callback('商品编码不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        goodsName: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.formValue.goodsName) {
                callback('商品名称不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        goodsType: [
          {
            required: true,
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (!this.formValue.goodsType) {
                callback('商品类型不能为空');
              } else {
                callback();
              }
            },
          },
        ],
        // limitNum: [
        //   {
        //     required: true,
        //     trigger: 'change',
        //     validator: (rule, value, callback) => {
        //       const reg = /^([0-9]|[1-9]\d+)(\d{0})?$/;
        //       const limitNum = this.formValue.limitNum;
        //       if (!limitNum) {
        //         callback('原价限购数量不能为空');
        //       } else if (!reg.test(limitNum)) {
        //         callback('请填写大于等于0的正整数');
        //       } else {
        //         callback();
        //       }
        //     },
        //   },
        // ],
        // limitFrequency: [
        //   {
        //     required: true,
        //     trigger: 'change',
        //     validator: (rule, value, callback) => {
        //       const reg = /^([0-9]|[1-9]\d+)(\d{0})?$/;
        //       const limitFrequency = this.formValue.limitFrequency;
        //       if (!limitFrequency) {
        //         callback('原价限购频率不能为空');
        //       } else if (!reg.test(limitFrequency)) {
        //         callback('请填写大于等于0的正整数');
        //       } else {
        //         callback();
        //       }
        //     },
        //   },
        // ],
        // activityPriceLimit: [
        //   {
        //     required: true,
        //     trigger: 'change',
        //     validator: (rule, value, callback) => {
        //       const reg = /^([0-9]|[1-9]\d+)(\d{0})?$/;
        //       const activityPriceLimit = this.formValue.activityPriceLimit;
        //       if (!activityPriceLimit) {
        //         callback('特价限购数量不能为空');
        //       } else if (!reg.test(activityPriceLimit)) {
        //         callback('请填写大于等于0的正整数');
        //       } else {
        //         callback();
        //       }
        //     },
        //   },
        // ],
        // minGross: [
        //   {
        //     required: true,
        //     trigger: 'change',
        //     validator: (rule, value, callback) => {
        //       // const reg  = /^([-+][0-9])(\.\d{1,2})?$/
        //       const reg = /^(([-+])?[1-9][0-9]*)(\.\d{1,2})?$/;
        //       const minGross = this.formValue.minGross;
        //       if (!minGross) {
        //         callback('最低毛利不能为空');
        //       } else if (!reg.test(minGross)) {
        //         callback('请填写最多2位小数的数字');
        //       } else {
        //         callback();
        //       }
        //     },
        //   },
        // ],
        autoPrice: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              if (!this.formValue.autoPrice) {
                callback('是否自动化调价不能为空');
              } else {
                callback();
              }
            },
          },
        ],
      },
      formLabelWidth: '130px',
      cellulars: [],
      onSubmitIng: false,
    };
  },
  watch: {
    formValueString(newValue) {
      if (!newValue) {
        return;
      }
      this.formValue = {
        ...this.formValue,
        ...parseToObject(newValue),
      };
    },
  },
  methods: {
    async onSubmit(name) {
      console.log(this.formValue);
      this.$refs[name].validate(async valid => {
        if (valid) {
          try {
            this.onSubmitIng = true;
            if (this.formValue.id) {
              await update(this.formValue);
            } else {
              await save(this.formValue);
            }
            this.onClose();
            this.resetTable();
            message('提示', '操作成功', 'success');
          } catch (e) {
            message('错误', e.message, 'error');
          }
          this.onSubmitIng = false;
        }
      });
    },
    resetTable() {
      this.$emit('resetTable');
    },
    onClose() {
      this.formValue = { ...INITIAL_FORM_VALUE };
      this.$refs.formRef.resetFields();
      this.$emit('close');
    },
    async goodsCodeBlur() {
      const { topChannelCode, goodsCode } = this.formValue;
      if (!topChannelCode) {
        message('提示', '请选择顶级渠道', 'warning');
        return;
      }
      if (!goodsCode) {
        this.formValue = { ...this.formValue, goodsName: '' };
        return;
      }
      try {
        const goodsName = await getGoodsName(topChannelCode, goodsCode);
        if (goodsName) {
          this.formValue = { ...this.formValue, goodsName };
          return;
        } else {
          message('错误', '商品编码不存在基础商品库存', 'error');
        }
      } catch (e) {
        message('错误', e.message, 'error');
      }
      this.formValue = { ...this.formValue, goodsName: '' };
    },
    topChannelChange() {
      this.formValue = {
        ...this.formValue,
        goodsCode: null,
        goodsName: null,
      };
      setTimeout(() => {
        this.$refs.formRef.clearValidate();
      }, 20);
    },
  },
  mounted() {},
};
